package org.mobiletrain.day19_toolbar;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
//这里需要注意的是AppTheme必须设置为NoActionBar否则会报错早已经存在ActionBar
public class MainActivity extends AppCompatActivity {
    // 工具栏
    private Toolbar toolbar;

    private ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.tool_bar);

        // 设置自定义Toolbar
        setSupportActionBar(toolbar);

        actionBar = getSupportActionBar();
        // 添加返回按钮
        actionBar.setDisplayHomeAsUpEnabled(true);
        // 展示图标
        actionBar.setDisplayShowHomeEnabled(true);
        // 展示标题
        actionBar.setDisplayShowTitleEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_tool, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.show_menu:
                Toast.makeText(MainActivity.this, "ToolBar", Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home:
                Toast.makeText(MainActivity.this, "Home", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    // Toolbar中按钮点击事件
    public void showToast(View view) {
        Toast.makeText(MainActivity.this, "Tollbar内部控件点击", Toast.LENGTH_SHORT).show();
    }

}
